package de.rac.vaadin.framework.ui;
/*******************************************************************************
 * Data for an edit form's password fields <br>
 * @author Volodymyr Dobryvechir (dobrivecher@yahoo.com)
 * @version 0.2 31.05.2012  <br>
 ******************************************************************************/
import com.vaadin.data.Item;
import com.vaadin.ui.Field;
import com.vaadin.ui.PasswordField;
import com.oros.db.*;
public class PasswordEditFormField extends EditFormField {
	
	private static final long serialVersionUID = 1L;
	private int firstColumnNr,secondaryColumnNr,originalColumnNr;
	private String NoMatchErrorMessage,NoMatchErrorMessageOrig;
	private String messageDigestAlgorithm;
	private boolean isValidated=false;
	private PasswordEditFieldValidator passwordEditFieldValidator;
     public PasswordEditFormField(String columnName,String value,String secondaryColumnName,String originalColumnName,
    		   String NoMatchErrorMessage,String messageDigestAlgorithm,AETableContainer container,ResourcePool resource)
     {
    	 super(columnName,value,true,resource);
    	 if (secondaryColumnName!=null)
    	 {
    	    this.secondaryColumnNr = container.getColumnIndex(secondaryColumnName);
    	    this.firstColumnNr = container.getColumnIndex(columnName);
    	    this.originalColumnNr = container.getColumnIndex(originalColumnName);
    	    this.NoMatchErrorMessageOrig = NoMatchErrorMessage;
    	    this.messageDigestAlgorithm = messageDigestAlgorithm;
    	    isValidated = true;
    	    this.NoMatchErrorMessage = resource.getTranslation(NoMatchErrorMessageOrig);
    	 }
     }
 	@Override
    public void notifyOfLocaleChange(ResourcePool resource,boolean activeEdit)
    {
    	super.notifyOfLocaleChange(resource, activeEdit);
    	if (NoMatchErrorMessageOrig!=null)
	            this.NoMatchErrorMessage = resource.getTranslation(NoMatchErrorMessageOrig);
        if (passwordEditFieldValidator!=null)
        	passwordEditFieldValidator.setNoMatchMessage(NoMatchErrorMessage);
    }
 	@Override
    public Field getFieldGenerated(Item item) 
    {
 		super.getFieldGenerated(item);
 		PasswordField pf = new PasswordField(caption);
 		AERawItem aeitem = (AERawItem)item;
 		pf.setRequired(aeitem.isNew());
 		if (isValidated)
 		{
 			passwordEditFieldValidator = new PasswordEditFieldValidator(firstColumnNr,secondaryColumnNr,originalColumnNr,aeitem,messageDigestAlgorithm,NoMatchErrorMessage);
 			pf.addValidator(passwordEditFieldValidator);
 		}
 		setCurrentField(pf);
 		return pf;
    }
}
